home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / O Boy / Source / Helpers_ut.cp < prev    next >
Encoding:
Text File  |  1996-06-21  |  4.1 KB  |  178 lines  |  [TEXT/R*ch]

  1. /*
  2.     Helpers_ut.cp
  3.     © 1996 Bob Boylan
  4.     
  5.     Revision History
  6.     MacHack 1996        initial creation
  7. */
  8. #include "Helpers_ut.h"
  9. #include "UAppleEventsMgr.h"
  10. #include "AEPrint.h"
  11. #include <algo.h>
  12. #include <string.h>
  13.  
  14. //    -----------------------------------------------------------------
  15. //    As4CharString
  16. //
  17. string
  18. As4CharString( Int_32 the4Chars )
  19. {
  20. union Mess
  21. {
  22.     char        theCharType[8];
  23.     DescType    theDescType;
  24. } Mess;
  25.  
  26.     Mess.theDescType = the4Chars;
  27.     Mess.theCharType[4] = '\0';
  28.  
  29.     return string( Mess.theCharType );
  30. }
  31.  
  32.  
  33. //    -------------------------------------------------------------
  34. //    AsInt32 - a direct descendant of powerplant extractor (ty MW)
  35. //
  36. Int_32
  37. AsInt32 ( const AEDesc &inDesc )
  38. {
  39.  
  40.     Handle    dataH;
  41.     AEDesc    coerceDesc = {typeNull, nil};
  42.  
  43.     if (inDesc.descriptorType == typeLongInteger) {
  44.         dataH = inDesc.dataHandle;        // Descriptor is the type we want
  45.         
  46.     } else {                            // Try to coerce to the desired type
  47.         if (AECoerceDesc(&inDesc, typeLongInteger, &coerceDesc) == noErr) {
  48.                                         // Coercion succeeded
  49.             dataH = coerceDesc.dataHandle;
  50.  
  51.         } else {                        // Coercion failed
  52.             throw (long) errAETypeError;
  53.         }
  54.     }
  55.     
  56.     Int_32 theRetVal = **(Int_32**) dataH;    // Extract value from Handle
  57.     if (coerceDesc.dataHandle != nil) {
  58.         AEDisposeDesc(&coerceDesc);
  59.     }
  60.     return theRetVal;
  61. }
  62.  
  63. //    -------------------------------------------------------------
  64. //    AsBoolean - a direct descendant of powerplant extractor (ty MW)
  65. //
  66. Boolean
  67. AsBoolean ( const AEDesc &inDesc )
  68. {
  69.  
  70.     Handle    dataH;
  71.     AEDesc    coerceDesc = {typeNull, nil};
  72.  
  73.     if (inDesc.descriptorType == typeBoolean) {
  74.         dataH = inDesc.dataHandle;        // Descriptor is the type we want
  75.         
  76.     } else {                            // Try to coerce to the desired type
  77.         if (AECoerceDesc(&inDesc, typeBoolean, &coerceDesc) == noErr) {
  78.                                         // Coercion succeeded
  79.             dataH = coerceDesc.dataHandle;
  80.  
  81.         } else {                        // Coercion failed
  82.             throw (long) errAETypeError;
  83.         }
  84.     }
  85.     
  86.     Boolean  theRetVal = **(Boolean**) dataH;    // Extract value from Handle
  87.     if (coerceDesc.dataHandle != nil) {
  88.         AEDisposeDesc(&coerceDesc);
  89.     }
  90.     return theRetVal;
  91. }
  92.  
  93. //    -----------------------------------------------------------------
  94. //    ListAsstring ... cute little routine to handle ae coercions a little better than gizmos
  95. //
  96. string
  97. ListAsstring( const AEDesc    &inDesc );
  98. string
  99. ListAsstring( const AEDesc    &inDesc )
  100. {
  101. string    theRetVal("[ ");
  102. OSErr     theErr;
  103. Int_32    theNItems = 0;
  104.     theErr = AECountItems(&inDesc, &theNItems);
  105.  
  106.     for( Int_32 theIndex = 1 ; theIndex <= theNItems ; theIndex++ )
  107.     {
  108.         {
  109.         StAEDescriptor    theNthItemD;
  110.         DescType        theBogusK;
  111.             theErr = AEGetNthDesc(&inDesc, theIndex, typeWildCard, &theBogusK, &theNthItemD.mDesc);
  112.             if( theNthItemD.mDesc.descriptorType == typeAEList )
  113.             {
  114.                 theRetVal += ListAsstring( theNthItemD.mDesc );
  115.             }
  116.             else
  117.             {
  118.                 theRetVal += Asstring( theNthItemD.mDesc );
  119.             }
  120.         }
  121.  
  122.         if( theIndex != theNItems )
  123.         {
  124.             theRetVal += ",";
  125.         }
  126.  
  127.  
  128.     }
  129.  
  130.     theRetVal += " ]";
  131.     
  132.     return theRetVal;
  133. }
  134.  
  135. //    -----------------------------------------------------------------
  136. //    Asstring
  137. //
  138. string
  139. Asstring ( const AEDesc    &inDesc )
  140. {
  141. // quick exit check
  142.     if( inDesc.descriptorType == typeNull ) return string("''");
  143.     if( inDesc.descriptorType == typeAEList ) return ListAsstring( inDesc );
  144.     
  145.     string    theRetVal;
  146.     Handle    dataH;
  147.     AEDesc    coerceDesc = {typeNull, nil};
  148.     
  149.     if (inDesc.descriptorType == typeChar) {
  150.         dataH = inDesc.dataHandle;        // Descriptor is the type we want
  151.     
  152.     } else {                            // Try to coerce to the desired type
  153.         if (AECoerceDesc(&inDesc, typeChar, &coerceDesc) == noErr) {
  154.                                         // Coercion succeeded
  155.             dataH = coerceDesc.dataHandle;
  156.  
  157.         } else {                        // Coercion failed ... use gizmos
  158.                 const UInt_32    theBUFFSIZE = 150;
  159.                 char    thePrintBuff[theBUFFSIZE];
  160.                 AEDesc    *theNonConstP = (AEDesc *) &inDesc;
  161.                 OSErr theErr = AEPrint( theNonConstP, thePrintBuff, theBUFFSIZE-1 );
  162.                 theRetVal = string( thePrintBuff, min( theBUFFSIZE,strlen(thePrintBuff) ) );
  163.         }
  164.     }
  165.     
  166.     if( theRetVal.length() == 0 )
  167.     {
  168.         Int_32    strLength = GetHandleSize(dataH);
  169.         if (strLength > 255) strLength = 255;
  170.         theRetVal = string( *dataH, strLength );
  171.     }
  172.         
  173.     if (coerceDesc.dataHandle != nil) {
  174.         AEDisposeDesc(&coerceDesc);
  175.     }
  176.     return theRetVal;
  177. }
  178.